package org.compiere.process;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.Mhrmbonus;
import org.compiere.model.MhrmbonusLine;
import org.compiere.model.Mhrmemployee;
import org.compiere.model.Mhrmorder;
import org.compiere.model.Mhrmreserve;
import org.compiere.model.Mhrmreservesectional;
import org.compiere.model.Mhrmsalaryemployee;
import org.compiere.model.X_hrm_bonusline_sub;
import org.compiere.model.X_hrm_order;
import org.compiere.model.X_hrm_salaryemployee;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.eevolution.model.X_HR_Employee;
import extend.org.compiere.hrm.TaxCompute;
import extend.org.compiere.hrm.TaxVariable;
import extend.org.compiere.hrm.TypePayment;

public class GenerateBonusSalary extends SvrProcess {

	private Mhrmbonus blist = null;
	private TaxVariable taxcomp = null;
	private Mhrmsalaryemployee mhrmsle = null;
	private double SUM = .0;
	private Timestamp dateFrom;	
	Timestamp sd = null;
	Timestamp ed = null;
	Calendar scalendar = Calendar.getInstance();
	Calendar ecalendar = Calendar.getInstance();
	/** */
	String closeTax = "N";
	
	/**	Employee List	*/
	private Mhrmemployee[] m_orderlines;
	private MhrmbonusLine hrmbl = null;	
	
	@Override
	protected void prepare() {
		
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++)
		{
				String name = para[i].getParameterName();
				if (name.equals("CloseTax"))
					closeTax = para[i].getParameter().toString();
				else
					log.log(Level.INFO, "Unknown Parameter: " + name);			
		}	
		
		// 
		blist = new Mhrmbonus(getCtx(), getRecord_ID(), null);
	}

	@Override
	protected String doIt() throws Exception {
		// 
		
		if (blist.haveCorrectDocument()) {
			return Msg.translate(Env.getCtx(), "Terminated");
		}
		Mhrmorder vorder = new Mhrmorder(getCtx(), blist.gethrm_order_ID(), null);
		dateFrom = blist.getDateFrom();		
		String aaa = Mhrmbonus.getDocAction(getCtx(), getRecord_ID());		
		//Mhrmbonus bmine = Mhrmbonus.get(getCtx(), getRecord_ID());
		m_orderlines = vorder.getLinesEmployee();
	
		if (aaa.equals(DocAction.ACTION_Complete)){
			for(int i=0; i < m_orderlines.length; i++){
				X_HR_Employee hremp = X_HR_Employee.getHR_Employee(m_orderlines[i].getC_BPartner_ID(), null);
					if (hremp.getC_BPartner_ID() == 0)
						throw new AdempiereException ("Info about employee was empty:" + m_orderlines[i].getC_BPartner().getName());
				hrmbl = MhrmbonusLine.getBonusline(hremp.getC_BPartner_ID(), blist.gethrm_bonus_ID(), dateFrom);				
				if (hrmbl == null)
					hrmbl = new MhrmbonusLine(Env.getCtx(), 0, null);				
				
				mhrmsle = Mhrmsalaryemployee.getSalary(hremp.getC_BPartner_ID(), dateFrom);
				BigDecimal amount = Mhrmsalaryemployee.getAmount(hremp.getC_BPartner_ID(), dateFrom);
				
				if (vorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Premium)){
					///SUM = m_orderlines[i].gethrm_amountbonus().doubleValue();
					if (amount != null){				
						if(m_orderlines[i].gethrm_percentBonus().doubleValue() > 0.)
							SUM = amount.doubleValue()*m_orderlines[i].gethrm_percentBonus().doubleValue()*0.01;
				    	if (m_orderlines[i].gethrm_amountbonus().doubleValue() > 0.)
							SUM = m_orderlines[i].gethrm_amountbonus().doubleValue();
					}
					
					hrmbl.setIsAverageSum(blist.isAverageSum());												
					hrmbl.sethrm_bonustype(vorder.gethrm_bonustype());
					
				}
//				else{
//					if (amount != null){				
//						if(m_orderlines[i].gethrm_percentBonus().doubleValue() > 0.)
//							SUM = amount.doubleValue()*m_orderlines[i].gethrm_percentBonus().doubleValue()*0.01;
//				    	if (m_orderlines[i].gethrm_amountbonus().doubleValue() > 0.)
//							SUM = m_orderlines[i].gethrm_amountbonus().doubleValue();
////				    }else{
////				    	throw new AdempiereException ("Info about amount was empty for person: " + hremp.getName() + ". Need to set salary or run timesheet procedure.");
//				    }
//					//
//					hrmbl.setIsAverageSum(blist.isAverageSum());												
//					hrmbl.sethrm_bonustype(vorder.gethrm_bonustype());
//				}
				
				hrmbl.sethrm_bonus_ID(blist.gethrm_bonus_ID());
				hrmbl.setC_BPartner_ID(hremp.getC_BPartner_ID());
				hrmbl.setHR_Department_ID(hremp.getHR_Department_ID());
				hrmbl.setHR_Job_ID(hremp.getHR_Job_ID());
				hrmbl.setIsSelected(blist.isSelected());
				hrmbl.setDateFrom(dateFrom);
				
				 /** Generate employee tax */
				taxcomp =  TaxCompute.GetTax(hremp.getC_BPartner_ID(), SUM, dateFrom, TypePayment.BONUS);
				//
				hrmbl.sethrm_IPN(new BigDecimal(taxcomp.getIPN()));
				hrmbl.sethrm_OPV(new BigDecimal(taxcomp.getOPV()));	
				//---add by A.Kulantayev
				if (vorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Premium)){
					
					if (!X_hrm_salaryemployee.HRM_SALARYTYPE_Direct.equals(mhrmsle.gethrm_salarytype())){
						
//						hrmbl.sethrm_amountbonus(new BigDecimal(taxcomp.getTOTAL() + taxcomp.getIPN() + taxcomp.getOPV()));
//						hrmbl.sethrm_total(new BigDecimal(taxcomp.getTOTAL()));
						hrmbl.sethrm_amountbonus(new BigDecimal(taxcomp.getTOTAL()));
						hrmbl.sethrm_total(new BigDecimal(taxcomp.getTOTAL()+ taxcomp.getIPN() + taxcomp.getOPV()));
					}
					else{
						
						hrmbl.sethrm_amountbonus(new BigDecimal(taxcomp.getTOTAL()));
						hrmbl.sethrm_total(new BigDecimal(taxcomp.getTOTAL()));
					}
				}
				//---

//---------------------Old code 15/07/2013  16:09 p.m.------------------------------------------------------------------------				
//				if (vorder.gethrm_ordertype().equals(X_hrm_order.HRM_ORDERTYPE_Premium)){
//					
//					hrmbl.sethrm_amountbonus(new BigDecimal(taxcomp.getTOTAL() + (taxcomp.getIPN() + taxcomp.getOPV())));
//					hrmbl.sethrm_total(new BigDecimal(taxcomp.getTOTAL() + (taxcomp.getIPN() + taxcomp.getOPV())));
//					
//				}else if (X_hrm_salaryemployee.HRM_SALARYTYPE_Direct.equals(mhrmsle.gethrm_salarytype())){
//					
//					hrmbl.sethrm_amountbonus(new BigDecimal(taxcomp.getTOTAL()));
//					hrmbl.sethrm_total(new BigDecimal(taxcomp.getTOTAL()));
//					
//				}else{
//					
//					hrmbl.sethrm_amountbonus(new BigDecimal(taxcomp.getTOTAL() + (taxcomp.getIPN() + taxcomp.getOPV())));
//					hrmbl.sethrm_total(new BigDecimal(taxcomp.getTOTAL()));
//				}
//-----------------------------------------------------------------------------------------------------------------------------				
				// Set social tax variables
				hrmbl.sethrm_COtch(new BigDecimal(taxcomp.getCOTCH()));
				hrmbl.sethrm_CNalog(new BigDecimal(taxcomp.getCNALOG()));
				
				if (blist.isSelected()){					
//					hrmbl.sethrm_reserve_ID(Mhrmreserve.RESERVE_BONUS);
//					hrmbl.sethrm_reservesectional_ID(Mhrmreserve.RESERVE_SECTIONAL_CHARGE);
//					hrmbl.sethrm_reservesectionaln_ID(Mhrmreserve.RESERVE_SECTIONAL_CNALOG);
//					hrmbl.sethrm_reservesectionalo_ID(Mhrmreserve.RESERVE_SECTIONAL_COTCH);
					// create sub line with reserve link
					Mhrmreservesectional[] line = Mhrmreservesectional.getMhrmreservesectional(); 
					for(Mhrmreservesectional l: line){
						X_hrm_bonusline_sub bonusLine = MhrmbonusLine.getBonusLineAcct(hrmbl.gethrm_bonusLine_ID(), l.gethrm_reservesectional_ID());
						if(bonusLine == null)
							bonusLine = new X_hrm_bonusline_sub(Env.getCtx(),0,null);
						
						if(l.gethrm_reservesectional_ID() == Mhrmreserve.RESERVE_SECTIONAL_CHARGE){
							bonusLine.sethrm_amount(hrmbl.gethrm_total());
							bonusLine.sethrm_reservesectional_ID(Mhrmreserve.RESERVE_SECTIONAL_CHARGE);
						}
						else if(l.gethrm_reservesectional_ID() == Mhrmreserve.RESERVE_SECTIONAL_CNALOG){
							bonusLine.sethrm_amount(hrmbl.gethrm_CNalog());
							bonusLine.sethrm_reservesectional_ID(Mhrmreserve.RESERVE_SECTIONAL_CNALOG);
						}
						else if(l.gethrm_reservesectional_ID() == Mhrmreserve.RESERVE_SECTIONAL_COTCH){
							bonusLine.sethrm_amount(hrmbl.gethrm_COtch());
							bonusLine.sethrm_reservesectional_ID(Mhrmreserve.RESERVE_SECTIONAL_COTCH);
						}
						bonusLine.sethrm_reserve_ID(Mhrmreserve.RESERVE_BONUS);
						bonusLine.sethrm_bonus_ID(blist.gethrm_bonus_ID());
						bonusLine.sethrm_bonusLine_ID(hrmbl.gethrm_bonusLine_ID());
						bonusLine.saveEx();
					}			
				}
				
				 if (closeTax.equals("Y"))
					 hrmbl.setProcessed(true);
				hrmbl.saveEx();						    
			}
		
		if (closeTax.equals("Y")){
			blist.setProcessed(true);				
			blist.saveEx();
		}	
		
		}
		
		return Msg.translate(Env.getCtx(), "Success");
	
	}
}
